﻿using DevComponents.DotNetBar;
using Public;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Configuration;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Runtime.Remoting.Contexts;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace Matter_Barcode
{
    public partial class Frm_Data : Form
    {
        public Frm_Data()
        {
            InitializeComponent();
        }
        static MySQLHelper DBhelper;
        string[,] cell_query_dgv = new string[,] { { "id", "ID" }, { "sn", "SN" },
        { "model", "型号" },{"status","状态"},
        { "create_time", "创建时间" },};
        private void buttonX_query_Click(object sender, EventArgs e)
        {
            try
            {
                System.Windows.Forms.Application.DoEvents();
                string sql_ii = string.Format(@"select 
                id,sn,model,
                CASE status   
                        WHEN 1 THEN '有效'   
                        WHEN 2 THEN '失效'   
                        ELSE '未知状态'
                    END AS status  ,create_time
                from print_rec t where 1=1  ");
                if (sn.Text.Trim() + "" != "")
                {
                    sql_ii += " and t.sn='" + sn.Text.Trim() + "' ";
                }
                if (model.Text.Trim() + "" != "")//按
                {
                    sql_ii += " and t.model='" + model.Text.Trim() + "' ";
                }
                if (dateX_CREATE_DATE.LockUpdateChecked == true)
                {
                    sql_ii += " and t.create_time >='" + Tools.FormatDateTime(dateX_CREATE_DATE.Value) + "' ";
                }
                if (dateX_CREATE_DATE2.LockUpdateChecked == true)
                {
                    sql_ii += " and t.create_time<='" + Tools.FormatDateTime(dateX_CREATE_DATE2.Value) + "' ";
                }
                sql_ii += " limit 10000 ";
                DataTable dt_query = DBhelper.SelectDataBase(sql_ii);
                dataGridViewX1.DataSource = dt_query;
                groupPanel1.Text = "查询结果明细：" + dataGridViewX1.RowCount + "条记录";
                dataGridViewX1.Columns["create_time"].DefaultCellStyle.Format = "yyyy-MM-dd HH:mm:ss";
            }
            catch (Exception ex)
            {

                Tools.ShowError("查询异常:\n" + ex.Message);
            }
        }

        private void Frm_Data_Load(object sender, EventArgs e)
        {
            string conn = ConfigurationManager.AppSettings["Connectionstring"] + "";
            DBhelper = new MySQLHelper(conn);
            Tools.initDataView2(dataGridViewX1, cell_query_dgv);//装载表头列标题

            
            dateX_CREATE_DATE.Value = DateTime.Now.AddDays(-2).Date;
            dateX_CREATE_DATE2.Value = DateTime.Now.AddDays(+1).Date;

            //label_barcode1.Text = ConfigurationManager.AppSettings["label_barcode1"] + "";
            //label_barcode2.Text = ConfigurationManager.AppSettings["label_barcode2"] + "";
        }

        private void dataGridViewX1_RowPostPaint(object sender, DataGridViewRowPostPaintEventArgs e)
        {
            try
            {
                Rectangle rectangle = new Rectangle(e.RowBounds.Location.X,
                    Convert.ToInt32(e.RowBounds.Location.Y + (e.RowBounds.Height - dataGridViewX1.RowHeadersDefaultCellStyle.Font.Size) / 2),
                    dataGridViewX1.RowHeadersWidth - 4, e.RowBounds.Height);
                TextRenderer.DrawText(e.Graphics, (e.RowIndex + 1).ToString(),
                    dataGridViewX1.RowHeadersDefaultCellStyle.Font, rectangle, dataGridViewX1.RowHeadersDefaultCellStyle.ForeColor,
                    TextFormatFlags.Right);
            }
            catch (Exception ex)
            {
                Console.Write("dgv1_RowPostPaint：" + ex.Message);
            }
        }

        private void btn_out_excel_Click(object sender, EventArgs e)
        {
            Tools.Export_Excel(dataGridViewX1, this.Text);
        }

        private void dataGridViewX1_CellMouseDown(object sender, DataGridViewCellMouseEventArgs e)
        {
            if (e.Button == MouseButtons.Right)
            {
                if (e.RowIndex >= 0)
                {
                    //若行已是选中状态就不再进行设置
                    if (dataGridViewX1.Rows[e.RowIndex].Selected == false)
                    {
                        dataGridViewX1.ClearSelection();
                        dataGridViewX1.Rows[e.RowIndex].Selected = true;
                    }
                    //只选中一行时设置活动单元格
                    if (dataGridViewX1.SelectedRows.Count == 1)
                    {
                        dataGridViewX1.CurrentCell = dataGridViewX1.Rows[e.RowIndex].Cells[e.ColumnIndex];
                    }
                    //弹出操作菜单
                    contextMenuStrip1.Show(MousePosition.X, MousePosition.Y);
                }
            }
        }

        private void 失效标签ToolStripMenuItem_Click(object sender, EventArgs e)
        {
            DialogResult dr = MessageBox.Show(this, "确认要失效吗？", "提示", MessageBoxButtons.YesNo, MessageBoxIcon.Question);
            if (dr != DialogResult.Yes)
            {
                return;
            }
            string qrcode = dataGridViewX1.CurrentRow.Cells["sn"].Value != null ? dataGridViewX1.CurrentRow.Cells["sn"].Value.ToString() : "";
            
            string sql2 = string.Format(@"UPDATE print_rec SET status=2,update_time=now() WHERE  sn='{0}'   ", qrcode);
            int res = DBhelper.ExecuteSql(sql2);
            if (res <= 0)
            {
                Tools.ShowError(string.Format(@"更新失败"));
                return;
            }
            else
            {
                Tools.ShowInformation(string.Format(@"更新成功"));
            }
        }

        private void dataGridViewX1_DataSourceChanged(object sender, EventArgs e)
        {
            for (int i = 0; i < dataGridViewX1.Rows.Count; i++)
            {
                string CY = dataGridViewX1.Rows[i].Cells["status"].Value.ToString();
                if (CY == "有效")
                {
                    dataGridViewX1.Rows[i].Cells["status"].Style.ForeColor = Color.Green;
                    //dataGridViewX1.Rows[i].DefaultCellStyle.ForeColor = System.Drawing.Color.Green;
                }
                if (CY == "失效")
                {
                    dataGridViewX1.Rows[i].DefaultCellStyle.ForeColor = System.Drawing.Color.Red;
                }
            }
        }
    }
}
